<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */

namespace App\Http\Admin\Company\Controllers;
use Framework\BaseClass\Http\Admin\Controller;
use App\Eloquent\Ygt\Company;
use App\Eloquent\Ygt\Department;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\User;

class UserController extends Controller
{
    public function index()
    {
    }
    public function lists()
    {
        if (request()->isMethod('post')) {

            $pageSize       = request('pageSize', 30);
            $pageCurrent    = request('pageCurrent', 1);
            $offset         = ($pageCurrent - 1) * $pageSize;

            $truename       = request( 'truename', '' );
            $mobile         = request( 'mobile', '' );

            $where          = [];
            if( $truename !== '' ){
                $where['truename']      = ['like','%'.$truename.'%'];
            }
            if( $mobile !== '' ){
                $where['mobile']         = ['like','%'.$mobile.'%'];
            }
            $company_col   = Company::getList( '','id,title' );
            $company_arr   = $company_col->pluck( 'title','id' );
            $user_col      = User::getList( $where,'',$pageSize,$offset );
            $count          = User::getCount($where);
            $lists          = $user_col->toArray();
            $user_id_arr    = [];
            foreach( $lists as $key=>$val )
            {
                $company_id                         = $val['company_id'];
                $company_title                      = '--';
                if( $company_id && isset($company_arr[$company_id]) )
                {
                    $company_title                  = $company_arr[$company_id];
                    $user_id_arr[]                  = $val['id'];
                }
                $lists[$key]['company_title']     = $company_title;
            }
            $where                                  = ['user_id'=>['in',$user_id_arr]];
            $department_user                        = DepartmentUser::getList( $where,'user_id,department_id' );
            $department_user_list                   = $department_user->toArray();
            $department_id_arr                      = [];
            foreach( $department_user_list as $key=>$val )
            {
                $department_id_arr[]                = $val['department_id'];
            }
            $where                                  = ['id'=>['in',$department_id_arr]];
            $department                             = Department::getList( $where,'id,title' );
            $department_arr                         = $department->pluck( 'title','id' );
            foreach( $lists as $key=>$val )
            {
                $user_id                            = $val['id'];
                $department_title                   = '--';
                if( $user_id && isset($department_arr[$user_id]) )
                {
                    $department_title               = $department_arr[$user_id];
                }
                $lists[$key]['department_title']  = $department_title;
            }
            $data           = ['totalRow'=>$count,'pageCurrent'=>$pageCurrent,'list'=>$lists];
            $data_json      = json_encode($data);
            return $data_json;
        }
        return $this->view('lists' );
    }
    public function set()
    {
    }
    public function delete()
    {
        $id                 = request( 'id', 0 );
        if( $id ){
            $id_arr         = explode(',', $id);
            $where          = ['id'=>['in',$id_arr]];
            $del_result     = User::del($where);
            if( $del_result !== false ){
                return $this->ajaxSuccess('成功');
            } else {
                return $this->ajaxFail('失败1');
            }
        } else {
            return $this->ajaxFail('失败2');
        }
    }
}